import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { STColumn, STComponent } from '@delon/abc'
import { Route, ActivatedRoute, Router } from '@angular/router'
import { I18NService } from '@core'
import { RuleTypeTextMap } from '@shared/interfaces/common'
import { RiskRules } from '@shared/interfaces/post-risk-control'
import { PostRiskStoreService } from '../../new/post-risk-store.service'
import { ListBaseClass } from '@shared/classes/ListBaseClass'
import { IQueryParams } from '@shared/interfaces/queryParams'
import { IRiskControlBiz } from '@shared/interfaces/post-risk-control'
import { NzMessageService } from 'ng-zorro-antd'
import { DataService } from '@shared/services/data.service'
import { CurrencyService } from '@shared/services/currency.service'
import { numScale } from 'app/utils/helpers'

@Component({
  selector: 'app-tab-rules',
  templateUrl: './tab-rules.component.html',
  styleUrls: ['../../../../shared/styles/table.less'],
})
export class TabRulesComponent extends ListBaseClass<IRiskControlBiz, IQueryParams> implements OnInit {
  @ViewChild('tplControl') tplControl: TemplateRef<void>
  riskRules: RiskRules[]
  data: any
  RuleTypeTextMap = RuleTypeTextMap
  currency: string
  digital: number
  columns: STColumn[] = [
    {
      title: '规则编号',
      i18n: 'RISK_CTRL.RULE.ID',
      index: 'ruleID',
      width: '50px',
      className: 'text-center',
      fixed: 'left',
    },
    {
      title: '规则属性',
      i18n: 'GENERAL.INFO.RULE_TYPE',
      index: 'ruleType',
      width: '100px',
      className: 'text-center',
      render: 'ruleType',
    },
    {
      title: '规则描述',
      i18n: 'RISK_CTRL.RULE.DES',
      index: 'ruleName',
      width: '250px',
      className: 'text-center',
    },
    {
      title: '阈值描述',
      i18n: 'GENERAL.INFO.RULE_THRE_DESC',
      index: 'thresholdDesc',
      width: '200px',
      className: 'text-center',
    },
    {
      title: '阈值',
      i18n: 'GENERAL.INFO.RULE_THRE',
      index: 'threshold',
      width: '100px',
      className: 'text-center',
    },
    {
      title: '分值',
      i18n: 'GENERAL.INFO.SCORE',
      index: 'score',
      width: '100px',
      className: 'text-center',
      fixed: 'right',
    },
  ]
  constructor(
    public msg: NzMessageService,
    public route: ActivatedRoute,
    public router: Router,
    public i18n: I18NService,
    public riskStore: PostRiskStoreService,
    private dataService: DataService,
    private currencyService: CurrencyService,
  ) {
    super(msg, i18n, route, router)
  }

  ngOnInit() {
    this.currency = this.dataService.currentPostRisk$.value.currency
    this.currencyService.getDecimalByCode(this.currency).subscribe(digital => {
      this.digital = digital
    })
    this.data = this.dataService.currentPostRisk$.value.ruleList || this.dataService.origPostRisk$.value.ruleList
    this.data.forEach(rule => {
      if (rule.thresholdType === '3') {
        rule.threshold = this.transAmt(rule.threshold, this.digital)
      }
    })
  }

  transAmt(value: number, digital: number = 0) {
    const amt = numScale(value, -digital).toFixed(digital)
    return amt
  }
}
